/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package geometry;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Rectangle {
	public int bbxi;
	public int bbyi;
	public int bbxf;
	public int bbyf;
	public Rectangle(int a, int b, int c, int d){
		bbxi = a;
		bbyi = b;
		bbxf = c;
		bbyf = d;
		sort();
	}

	public Rectangle() {
		bbxi = bbyi = bbxf = bbyf = 0;
	}
	public Rectangle(Rectangle r) {
		bbxi = r.bbxi;
		bbyi = r.bbyi;
		bbxf = r.bbxf;
		bbyf = r.bbyf;
	}
	public void sort() {
		if(bbxi > bbxf){
			int tmp = bbxi;
			bbxi = bbxf;
			bbxf = tmp;
		}
		if(bbyi > bbyf){
			int tmp = bbyi;
			bbyi = bbyf;
			bbyf = tmp;
		}
	}
	
	public String toString(){
		return "Rectangle: xi="+bbxi+" yi="+bbyi+" xf="+bbxf+" yf="+bbyf;
	}
	
	public void load(DataInputStream is) throws IOException {
		bbxi = is.readInt();
		bbyi = is.readInt();
		bbxf = is.readInt();
		bbyf = is.readInt();
		
	}
	public void unite(Rectangle r) {
		if(bbxi>r.bbxi)bbxi=r.bbxi;
		if(bbyi>r.bbyi)bbyi=r.bbyi;
		if(bbxf<r.bbxf)bbxf=r.bbxf;
		if(bbyf<r.bbyf)bbyf=r.bbyf;
	}
	
	public void intersect(Rectangle r){
		if(r.bbxi > bbxi)
			bbxi = r.bbxi;
		if(r.bbyi > bbyi)
			bbyi = r.bbyi;
		if(r.bbxf < bbxf)
			bbxf = r.bbxf;
		if(r.bbyf < bbyf)
			bbyf = r.bbyf;
	}
	public int getArea(){
		return getWidth()*getHeight();
	}
	
	public int getWidth() {
		return bbxf-bbxi;
	}
	public int getHeight() {
		return bbyf-bbyi;
	}
	public void unload(DataOutputStream os) throws IOException {
		 os.writeInt(bbxi);
		 os.writeInt(bbyi);
		 os.writeInt(bbxf);
		 os.writeInt(bbyf);
	}
	public final boolean colides(Point p) {
		return colides(p.x,p.y);
	}
	public final boolean colides(Point i, Point f) {
		return colides(i.x,i.y,f.x,f.y);
	}
	public final boolean colides(int x1, int y1, int x2, int y2) {
		return ((x1 <= bbxf) || (x2 <= bbxf))
	    	&& ((bbxi <= x1) || (bbxi <= x2))
	    	&& ((y1 <= bbyf) || (y2 <= bbyf))
	    	&& ((bbyi <= y1) || (bbyi <= y2));
	}
	public final boolean colides(int x, int y) {
		return x >= bbxi
			&& x <= bbxf
			&& y >= bbyi
			&& y <= bbyf;
	}
	
	public final boolean colides(Rectangle a) {
		return bbxi   <= a.bbxf
		    && a.bbxi <= bbxf
		    && bbyi   <= a.bbyf
		    && a.bbyi <= bbyf;
	}
	
	public int xcenter() {
		return (bbxf-bbxi)/2+bbxi;
	}

	public int ycenter() {
		return (bbyf-bbyi)/2+bbyi;
	}
}
